
var getTableNamesUrl = "../devtools/v1/common/tables";
var getTableColumnsApi = "../devtools/v1/common/columns";
var updateTableUrl =  "../devtools/v1/common/updatecolumns";
var createTableUrl = "../devtools/v1/maintain/table";
var getMetadataListUrl = "../devtools/v1/common/metadataList";
var addMetadataUrl = "../devtools/v1/common/metadata";
var bulidTableSqlUrl = "../devtools/v1/maintain/parseSql";
var vm = new Vue({
    el:'#app',
    data:{
        searchVal: "",
        tables: [],
        newDocuments:[],
        tableMessages: [],
        tableName: "", //表名
        dialogShow: false,
        SQL: "",   //语句创建表
        newTableName: "", //新建数据库的表名
        objName: "",  //表名称
        IdType: "DBAI",
        extdata:true,
        active:true,
        create_by:true,
        create_time:true,
        update_time:true,
        update_by:true,
        isActive:0,
        // tableFields: ["extdata", "active","create_by", "create_time", "update_time"],
        generateCommonCodes: "",
        fieldTypeList: ["String", "Long", "Integer", "Boolean", "Date", "Double"],
        metadataList: [],
        defaultConfig: {
            String: {
                length: 100,
                extList: [{
                    label: "char",
                    val: "fixed",
                    title: "固定长度(char)"
                }, {
                    label: "LIKE",
                    val: "like",
                    title: "支持模糊查询"
                }]
            },
            Long: {
                length: 11,
                label: "unsigned",
                val: "无符号正整数(不含+-号)",
                title: "无"
            },
            Integer: {
                length: 5,
                label: "unsigned",
                val: "无符号正整数(不含+-号)",
                title: "无"
            },
            Boolean: {
                length: 1,
                label: "",
                val: "",
                defaultList: [{
                    val: "",
                    label: "无"
                }, {
                    val: "1",
                    label:"true"
                }, {
                    val: "0",
                    label:  "false"
                }]
            },
            Date: {
                length: 0,
                label: "",
                title: "日期格式",
                extList: [{
                    label: "yyyy-MM-dd HH:mm",
                    val: "yyyy-MM-dd HH:mm"
                }, {
                    label: "yyyy-MM-dd",
                    val: "yyyy-MM-dd"
                }],
                defaultList: [{
                    val: "",
                    label:  "无"
                }, {
                    val: "CURRENT_TIMESTAMP",
                    label:  "当前时间"
                }]
            },
            Double: {
                length: 10,
                label: "",
                title: "小数位数是否固定",
                extList: [{
                    label: "浮点",
                    val: "float"
                }, {
                    label:"定点",
                    val:"fixed"
                }]
            }
        },
        newMetaDataConfig: {
            itemName: "",
            type: "",
            children: [{
                itemName: "",
                itemValue: ""
            }],
            comment: ""
        },
        tabActive:"table",
        editable: true

    },
    mounted: function() {
        this.getTableNames()
    },
    methods: {
        getTableNames: function() {
            var _this = this;
            $.ajax({
                url: getTableNamesUrl,
                type: "GET",
                dataType: "json",
                success: function(result) {
                    if(result.code === 0){
                        _this.tables=result.data;
                        _this.changeTables( _this.tables[0].tableName,_this.tables[0].editable);
                    }
                }
            })
        },
        changeTables: function(tableName,editable) {
            var _this = this;
            $.ajax({
                url: getTableColumnsApi+"?tableName="+tableName,
                type: "GET",
                dataType: "json",
                success: function(result) {
                    if(result.code === 0){
                        _this.tableMessages=result.data;
                        _this.isActive=tableName;
                        for(var i=0;i<_this.tableMessages.length;i++) {
                            if(_this.tableMessages[i].tableName==tableName){
                                _this.tableMessages[i].editable=editable;
                                _this.tableMessages[i].tableColumns[0]=_this.tableMessages[i].refColumn;
                                _this.tableMessages[i].tableColumns[1]=_this.tableMessages[i].asColumn;
                            }
                        }
                    }
                }
            })
        },
        buildTableSql: function() {
            var _this = this;
            $.ajax({
                url: bulidTableSqlUrl,
                type: "GET",
                dataType: "json",
                success: function(result) {
                    if(result.code === 0){
                        _this.tables=result.data;
                    }
                }
            })
        },
        // 新建元数组
        addMetaData: function() {
            var _this=this;
            $.ajax({
                url:addMetadataUrl,
                data:JSON.stringify({
                    itemName: _this.newMetaDataConfig.itemName,
                    type:_this.newMetaDataConfig.type,
                    children: _this.newMetaDataConfig.children,
                    }) ,
                type:"POST",
                dataType: "json",
                contentType: "application/json;charset-UTF-8",
                success: function(result) {
                    $("#metaModal").modal('hide');
                    toastr.options = {
                        closeButton: false,
                        debug: false,
                        progressBar: false,
                        positionClass: "toast-top-center",
                        onclick: null,
                        showDuration: "300",
                        hideDuration: "1000",
                        timeOut: "2000",
                        extendedTimeOut: "1000",
                        showEasing: "swing",
                        hideEasing: "linear",
                        showMethod: "fadeIn",
                        hideMethod: "fadeOut"
                    };
                    toastr.success(result.msg);
                }
            })
        },
        //新建表
        createNewTable:function () {
            var _this = this;
            if(_this.extdata==true){
                _this.extdata = "1"
            }else{
                _this.extdata = "0"
            };
            if(_this.active==true){
                _this.active = "1"
            }else{
                _this.active = "0"
            };
            if(_this.create_by==true){
                _this.create_by = "1"
            }else{
                _this.create_by = "0"
            };
            if(_this.create_time==true){
                _this.create_time = "1"
            }else{
                _this.create_time = "0"
            };
            if(_this.update_by==true){
                _this.update_by = "1"
            }else{
                _this.update_by = "0"
            };
            if(_this.update_time==true){
                _this.update_time = "1"
            }else{
                _this.update_time = "0"
            };
            if(_this.generateCommonCodes==true){
                _this.generateCommonCodes = "1"
            }else{
                _this.generateCommonCodes = "0"
            };
            $.ajax({
                url:createTableUrl,
                data:JSON.stringify({
                    newTableName: _this.newTableName,
                    objName:_this.objName,
                    sql: _this.SQL,
                    idType: _this.IdType,
                    extdata: _this.extdata,
                    active: _this.active,
                    create_by: _this.create_by,
                    create_time: _this.create_time,
                    update_by: _this.update_by,
                    update_time:  _this.update_time,
                    generateCommonCodes: _this.generateCommonCodes}) ,
                type:"POST",
                dataType: "json",
                contentType: "application/json;charset-UTF-8",
                success: function(result) {
                    $("#myModal").modal('hide');
                    toastr.options = {
                        closeButton: false,
                        debug: false,
                        progressBar: false,
                        positionClass: "toast-top-center",
                        onclick: null,
                        showDuration: "300",
                        hideDuration: "1000",
                        timeOut: "2000",
                        extendedTimeOut: "1000",
                        showEasing: "swing",
                        hideEasing: "linear",
                        showMethod: "fadeIn",
                        hideMethod: "fadeOut"
                    };
                    toastr.success('建表成功');
                    _this.getTableNames();
                    _this.extdata = true;
                    _this.active = true;
                    _this.create_by = true;
                    _this.create_time = true;
                    _this.update_by = true;
                    _this.update_time = true;
                    _this.generateCommonCodes = true;
                    _this.newTableName="";
                    _this.objName="";
                    _this.SQL="";
                    _this.IdType="DBAI";
                }
            })
        },
        // 新增行
        addNewRow:function (index) {
            var _this=this;
            var item={
                active:true,
                asColumn:"",
                colIndex: index+1,
                colLength: 100,
                colName: "",
                colType: "varchar(100)",
                decimal: 0,
                defaultValue: "",
                editable: true,
                extensionList: "",
                fieldLabel: "",
                fieldName: "extdata",
                fieldPackagingType: "String",
                formType: "INPUT",
                hasReference: false,
                hasReferenceTable: false,
                isInsert: 0,
                isList: 0,
                isQuery: 0,
                keyType: null,
                modify: false,
                must: false,
                notnull: 0,
                primaryKey: false,
                queryType: "",
                refColumn: "",
                refTable: "",
                refType: "",
                requireMoreSpace: false,
                tableName:_this.tableMessages[0].tableName,
                update: false,
            }
            _this.tableMessages.splice(index+1,0,item);
        },
        //修改表格
        updateThisTable:function(){
            var _this=this;
            var flag=0;
            toastr.options = {
                closeButton: false,
                debug: false,
                progressBar: false,
                positionClass: "toast-top-center",
                onclick: null,
                showDuration: "300",
                hideDuration: "1000",
                timeOut: "2000",
                extendedTimeOut: "1000",
                showEasing: "swing",
                hideEasing: "linear",
                showMethod: "fadeIn",
                hideMethod: "fadeOut"
            };
            $.ajax({
                    url:updateTableUrl,
                    data:JSON.stringify(_this.tableMessages) ,
                    type:"POST",
                    dataType: "json",
                    contentType: "application/json;charset-UTF-8",
                    success: function(result) {
                        if(result.msg=="更新成功！"){
                            toastr.success(result.msg);
                            _this.changeTables(_this.tableMessages[0].tableName,_this.tableMessages[0].editable);
                        }
                        else{
                            toastr.error(result.msg);
                        }
                        // _this.changeTables(_this.tableMessages[0].tableName,_this.tableMessages[0].editable);
                    }
                })
        },
        addDefault:function(fieldType,index){
            var _this=this;
            if(fieldType=="Long"){
                _this.tableMessages[index].colLength=11;
                _this.tableMessages[index].extensionList="unsigned";
                _this.tableMessages[index].formType="INPUT";
            }
            else if(fieldType=="Integer"){
                _this.tableMessages[index].colLength=5;
                _this.tableMessages[index].extensionList="unsigned";
                _this.tableMessages[index].formType="INPUT";
            }
            else if(fieldType=="Double"){
                _this.tableMessages[index].colLength=10;
                _this.tableMessages[index].formType="NONE";
                _this.tableMessages[index].extensionList="float";
            }
            else if(fieldType=="Boolean"){
                _this.tableMessages[index].colLength=1;
                _this.tableMessages[index].formType="SELECT";
            }
            else if(fieldType=="String"){
                _this.tableMessages[index].colLength=100;
                _this.tableMessages[index].formType="INPUT";
            }
            else if(fieldType=="Date"){
                _this.tableMessages[index].colLength=0;
                _this.tableMessages[index].extensionList="yyyy-MM-dd HH:mm";
                _this.tableMessages[index].formType="DATETIME_PICKER";
                _this.tableMessages[index].queryType="";
            }
        },
        dateShowType:function(extensionList,index){
            var _this=this;
            if(extensionList=="yyyy-MM-dd"){
                _this.tableMessages[index].formType="DATE_PICKER";
            }
            else if(extensionList=="yyyy-MM-dd HH:mm"){
                _this.tableMessages[index].formType="DATETIME_PICKER";
            }
        },
        changeFieldTable:function (refTableName,index) {
            var _this=this;
            $.ajax({
                url: getTableColumnsApi+"?tableName="+refTableName,
                type: "GET",
                dataType: "json",
                success: function(result) {
                    if(result.code === 0){
                        _this.tableMessages[index].tableColumns=[];
                        for(var i=0;i<result.data.length;i++)
                        {
                            _this.newDocuments[i]=result.data[i].colName;
                            _this.tableMessages[index].tableColumns[i]=_this.newDocuments[i];
                        }
                    }
                }
            })
        },
        // 新增元数组子项
        addItem:function(index){
            var _this=this;
            var item={
                itemName:"",
                itemValue:"",
            }
            if(_this.newMetaDataConfig.children[index].itemName==""||_this.newMetaDataConfig.children[index].itemValue=="")
            {
                toastr.options = {
                    closeButton: false,
                    debug: false,
                    progressBar: false,
                    positionClass: "toast-top-center",
                    onclick: null,
                    showDuration: "300",
                    hideDuration: "1000",
                    timeOut: "2000",
                    extendedTimeOut: "1000",
                    showEasing: "swing",
                    hideEasing: "linear",
                    showMethod: "fadeIn",
                    hideMethod: "fadeOut",
                };
                toastr.error("请先将子项填写完整再添加!");
            }
            else{
                _this.newMetaDataConfig.children.splice(index+1,0,item);
            }
            // _this.newMetaDataConfig.children.splice(index+1,0,item);
        },
        // 删除元数组子项
        removeItem:function (index) {
            var _this=this;
            _this.newMetaDataConfig.children.splice(index,1);
        },
        //改变关联选项
        changeRelType:function (refType,index) {
            var _this=this;
            if(refType=="M"){
                _this.tableMessages[index].refColumn="";
                _this.getMetaData();
                _this.tableMessages[index].formType="SELECT";
            }
            else if(refType=="T"){
                _this.tableMessages[index].refColumn="";
                _this.tableMessages[index].asColumn="";
                _this.tableMessages[index].refTable="";
            }
        },
        //获取元数组
        getMetaData:function () {
            var _this=this;
            $.ajax({
                url: getMetadataListUrl,
                type: "GET",
                dataType: "json",
                success: function(result) {
                    if(result.code === 0){
                        _this.metadataList=result.data;
                    }
                }
            })
        },
        // 删除表中某一行
        removeThisRow:function (index) {
            alert("您确定要删除第"+index+"行吗?");
            var _this=this;
            _this.tableMessages.splice(index,1);
        },
    }
});